from cocos import sprite
from constants import *
from utils import *

class Bicho( sprite.Sprite ):
    '''Base class for all bichos'''
    def __init__(self, image):
        super( Bicho, self).__init__(image)

        self.parent_map = None
        self.board = None

    def update(self):
        self.checkTeleportCell()
        self.set_direction()
        self.set_move()
        if self.pressed:
            self.dir_prop = self.dir_prop_copy

    def get_target_cell(self):
        if self.target:
            return self.parent_map.get_at_pixel(self.target.x, self.target.y)
        else:
            return None
    
    def get_current_cell(self):
        return self.parent_map.get_at_pixel(self.x,self.y)

    def get_next_cell(self):
        current_cell = self.get_current_cell()
        #print self.dir_prop.x
        return self.parent_map.get_cell(current_cell.i + self.dir_prop.x, current_cell.j + self.dir_prop.y)

    def get_next_cell_dir(self):
        current_cell = self.get_current_cell()
        #print self.dir_prop.x
        return self.parent_map.get_cell(current_cell.i + self.dir.x, current_cell.j + self.dir.y)
    
    def set_target(self, target):
        '''' Set target '''
        self.target = target

    def set_parent_map(self, mapa):
        '''' Set target '''
        self.parent_map = mapa
        self.board = tiledmap2board(self.parent_map, BOARD_START_POINT, BOARD_END_POINT, 'wall')
    
    def init_parent_map(self):
        self.cell = self.get_current_cell()
        self.current_cell = [self.cell.i,self.cell.j]
        self.next_cell = self.current_cell

    def checkTeleportCell(self):
        c = self.get_current_cell()
        t = c.get('teleport')
        if t:
            if c.i == 5:
                self.x = 24*TILE_SIZE
            else:
                self.x = 6*TILE_SIZE

class Pacman( Bicho ):
    def __init__(self, image):
        super( Pacman, self).__init__(image)

        self.pressed=False
        self.dir_prop_copy=None
        self.speed = 2.9
        self.x = (15*TILE_SIZE)+HALF_TILE_SIZE
        self.y = (6*TILE_SIZE)+HALF_TILE_SIZE

        # Aca mejor defino la velocidad como un vector de 2 componentes
        self.vel = Point2(-self.speed, 0)
        self.dir = Point2(-1,0)
        self.dir_prop = Point2(-1,0)

    def doblando(self):
        return (self.vel.x != 0 and self.vel.y !=0)
         
    

    def set_sentido(self):
        # yendo horizontal
        current_cell = self.get_current_cell()
        if self.dir.x != 0:
            dist = current_cell.center[0] - self.x
            if dist > 0:
                if self.dir.x < 0:
                    self.dir.x = self.dir.x * -1
                    self.vel.x = self.speed * self.dir.x
            elif dist < 0:
                if self.dir.x > 0:
                    self.dir.x = self.dir.x * -1
                    self.vel.x = self.speed * self.dir.x
            self.dir.y = self.dir_prop.y
        elif self.dir.y != 0:
            dist = current_cell.center[1] - self.y
            if dist > 0:
                if self.dir.y < 0:
                    self.dir.y = self.dir.y * -1
                    self.vel.y = self.speed * self.dir.y
            elif dist < 0:
                if self.dir.y > 0:
                    self.dir.y = self.dir.y * -1
                    self.vel.y = self.speed * self.dir.y
            self.dir.x = self.dir_prop.x


    def set_direction(self):
        
        next_cell = self.get_next_cell()
        if not (self.dir_prop.y == self.dir.y and self.dir_prop.x == self.dir.x) and not self.doblando():
            #print next_cell.i,next_cell.j,next_cell.get('wall')
            if(not next_cell.get('wall')):
                if(self.dir.dot(self.dir_prop) == 0):
                    # estamos doblando, direccion propuesta es perpendicular a la actual
                    self.set_sentido()
                    self.vel.x += self.speed * self.dir_prop.x
                    self.vel.y += self.speed * self.dir_prop.y
                else:
                    self.dir = self.dir_prop                    
                    self.vel.x = self.speed * self.dir.x
                    self.vel.y = self.speed * self.dir.y
            else:
                self.dir_prop=self.dir

    def set_move(self):
        
        current_cell = self.get_current_cell()
        next_cell= self.get_next_cell()
        if(self.dir.x != 0 and self.dir.y !=0):
        # si vamos en diagonal, o sea doblando
            if (self.dir_prop.y != 0):
                # si dir_pror es arriba o abajo, vertical, frenamos la x
                distx = abs(current_cell.center[0] - self.x)
                if(distx < self.speed):
                    self.x = current_cell.center[0]
                    self.vel.x = 0
                
            else:
                # si dir_prop es izquierda o derecha, horizontal, frenamos la y
                disty = abs(current_cell.center[1] - self.y)
                if(disty < self.speed):
                    self.y = current_cell.center[1]
                    self.vel.y = 0
        else:
            # si vamos en horizontal o vertical
            if(next_cell.get('wall')):
                distx = abs(current_cell.center[0] - self.x)
                disty = abs(current_cell.center[1] - self.y)
                if(distx < self.speed):
                    self.x = current_cell.center[0]
                    self.vel.x = 0
                if(disty < self.speed):
                    self.y = current_cell.center[1]
                    self.vel.y = 0
        self.position = (self.x + self.vel.x,self.y + self.vel.y)